{% extends "admin_base.html" %}
{% block title %}All Posts of all types{% endblock %}
{% block main %}
<div class="row">
    <!-- <div class="col-md-10"> -->
    <!-- <h1>New Post</h1> -->
    <a href="."><span class="label label-default">All</span></a>&nbsp;
    {% for post_type in post_types %}
        <a href="?type={{ post_type }}" class="post-tag">
            {% if post_type == cur_type %}
            <span class="label label-primary">
                {{ post_type }}
                <small><span class="glyphicon glyphicon-remove small rm-filter" aria-hidden="true"></span></small>
            </span>
            
            {% else %}
            <span class="label label-default">
                {{ post_type }} 
            </span>
            
            {% endif %}
            
        </a>
        &nbsp;
    {% endfor %}
    
    <table class="table table-striped">
        <thead>
            <tr>
                <th>No.</th>
                <th style="width: 15%">Title</th>
                <th>Abstract</th>
                <th>Author</th>
                <th style="width: 10%">Publish Date</th>
                <th style="width: 10%">Update Date</th>
                <th style="width: 5%">PostType</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for post in posts.items %}
            <tr id="post-{{ post.id }}">
                <td>{{ loop.index }}</td>
                <td>
                    <a href="{{ url_for('blog_admin.su_post_edit', slug=post.slug) }}" target="_blank">{{ post.title }}</a>              
                </td>
                <td>{{ post.abstract }}</td>
                <td>{{ post.author.username }}</td>
                <td>{{ post.pub_time.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>{{ post.update_time.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>{{ post.post_type }}</td>
                <td>
                    <a href="{{ url_for('blog_admin.su_post_edit', slug=post.slug) }}"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> </a>
                    &nbsp
                    <a href="{{ url_for('blog_admin.edit_post', slug=post.slug) }}"  class="del-post"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </a>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="8" style="text-align: left">No posts yet</td></tr>
            {% endfor %}      
        </tbody>
    </table>

    {% import '_pagination.html' as pagination %}
    {{ pagination.render_pagination(posts) }}

    <!-- </div> -->
    <!-- <div class="col-md-2">.col-md-2</div> -->
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
$('.del-post').on('click', function (event) {
    // var answer = confirm("Delete post?");
    // return answer;
    var url = $(this).attr('href');
    url = url + '?ajax=true';
    bootbox.confirm("Are you sure?", function(result) {
      if(result){
        // $.get(url, function(){
        //     location.reload();
        // });
        $.ajax({
            url: url,
            type: 'DELETE',
            success: function(result){
                location.reload();
            }

        })
        }
    });
    return false;
  });

$('.rm-filter').on('click', function (event) {

    location.href = "{{ url_for('blog_admin.su_posts') }}";
    return false;
  });
</script>

{% endblock %}